import store from '@/store'

let mutationObserver
export default {
  auth: {
    mounted: function (el, binding, vnode) {
      if (!store.userApp.permissionList.some((v: string) => v === binding.value)) {
        const comment = document.createComment(' ')
        Object.defineProperty(comment, 'setAttribute', {
          value: () => undefined
        })
        vnode.elm = comment
        vnode.text = ' '
        vnode.isComment = true
        el.parentNode.replaceChild(comment, el)
      }
    },
    MutationObserver: {
      mounted: function (el, binding, vnode) {
        const MutationObserver =
          window.MutationObserver || window.webkitMutationObserver || window.MozMutationObserver
        mutationObserver = new MutationObserver(function (mutations) {
          console.log(mutations)

          const types = ['childList', 'attributes', 'characterData']
          const arg = binding.arg
          if (arg && types.includes(arg)) {
            // 参数存在 执行参数类型的回调
            mutations.map((v) => {
              if (v.type === arg) {
                binding.value(el)
              }
            })
          } else {
            // 参数不存在 执行全部回调
            binding.value(el)
          }
        })
        mutationObserver.observe(el, {
          subtree: true,
          attributes: true,
          childList: true,
          characterData: true
        })
      },
      unmounted: function (el) {
        mutationObserver.disconnect()
      }
    }
  }
}
